<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function setting(User $user){
        return view('user.userSetting',compact('user'));
    }
    public function settingStore(Request $request,User $user){
//        dd($request->all());
        $this->validate($request,[
            'name'=>'bail|required|min:3|max:10|',
        ]);
        $name=$request->input('name');
        if($name!=$user->name){
            if(User::where('name',$name)->count()>0){
                return back()->withErrors('用户名已存在');
            }
            $user->name=$name;
        }
        //图片处理
        if($request->hasFile('avatar')&&$request->file('avatar')->isValid()){
            $filePath = upload($request, 'avatar', 'images/' . date('Ymd'));
            $thumb_name = thumb($filePath);
            $user->avatar='/storage/' . $filePath;
            $user->thumb=$thumb_name;
            $user->save();
            return redirect('user/'.$user->id.'/setting')->with('message','成功修改');
        }
    }
}
